package chapter02.part02;

import chapter02.FileUtil;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 读写 Zip 文档
 * 
 * @author insight
 * @since 2021/8/13
 */
public class ZipFileTest {
    public static void main(String[] args) throws IOException {
        // 写 Zip 的基本框架
        // 1. 创建输出流
        // 2. 对每个文件创建 ZipEntry
        // 3. 使用 putEntry()
        // 4. closeEntry()
        ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(
                "chapter02-io/src/main/resources/test.zip"
        ));
        ZipEntry zipEntry = new ZipEntry(FileUtil.TEST_FILE_PATH);
        zout.putNextEntry(zipEntry);
        zout.close();
        
        // 读
        // 1. 输入流
        // 2. getNextEntry()
        // 3. clostEntry()
        ZipInputStream zin = new ZipInputStream(new FileInputStream(
                "chapter02-io/src/main/resources/test.zip"
        ));
        while ((zipEntry = zin.getNextEntry()) != null) {
            System.out.println(zipEntry.getName());
            zin.closeEntry();
        }
        zin.close();
    }
}